package com.laobiao.file_netty_lib.packet.command;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import com.laobiao.file_netty_lib.exception.FileBaseException;
import com.laobiao.file_netty_lib.util.BinaryConvertUtil;

/**
 *  命令反馈
 * @author 38079
 *
 */
public class ResoultCommand extends BaseCommandPacket{
	
	
	/**
	 * 状态码,每种命令中返回的状态码可能都不相同，状态码表示当前命令执行到哪一个流程了，例如上传：4、代表客户端上传阶段。-4、代表服务端校验阶段，具体参阅文档
	 */
	private int code;
	/**
	 * 结果状态
	 */
	private boolean status;
	/**
	 * 反馈内容
	 */
	private String message;
	
	/**
	 * 构建回调消息包
	 * @param code 状态码
	 * @param message 消息内容
	 * @param status 结果成功与否
	 * @param businessGroup 业务组
	 */
	public ResoultCommand(int code,String message,boolean status,Integer businessGroup) {
		super();
		setCommand(PacketCommandType.RESOULT);
		if(businessGroup!=null) {
			setBusinessGroup(businessGroup);
		}
		this.code=code;
		this.message=message;
		this.status=status;
		
	}
	
	public ResoultCommand(byte[] errBytes) {
		ByteBuffer buf = setBaseCommand(errBytes);
		status=getStatusByByte(buf.get());
		byte[] b=new byte[4];
		//状态码
		buf.get(b);
		code=BinaryConvertUtil.byteArrayToInt(b);
		//消息长度
		buf.get(b);
		int msgSize=BinaryConvertUtil.byteArrayToInt(b);
		//消息
		b=new byte[msgSize];
		buf.get(b);
		try {
			message=new String(b,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new FileBaseException("异常消息解析错误！",e, FileBaseException.ERR_PARAM_WRONG);
		}
	}
	
	@Override
	protected byte[] getCommContentBytes() {
		ByteBuffer buf=ByteBuffer.allocate(1014);
		buf.put(getStatusByte());//执行状态
		buf.put(BinaryConvertUtil.intToByteArray(code));//状态码
		try {
			byte[] msgBytes = message.getBytes("UTF-8");
			buf.put(BinaryConvertUtil.intToByteArray(msgBytes.length));
			buf.put(msgBytes);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			throw new FileBaseException("异常消息编码错误！", e, FileBaseException.ERR_PARAM_WRONG);
		}
		return buf.array();
	}

	@Override
	public CommandCirculationType circulationType() {
		// TODO Auto-generated method stub
		return CommandCirculationType.PROCEDURE;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	private boolean getStatusByByte(byte b) {
		if((int)b==1) {
			return true;
		}else {
			return false;
		}
	}
	private byte getStatusByte() {
		if(status) {
			return 1;
		}else {
			return 0;
		}
	}
	
	@Override
	public String toString() {
		return "ResoultCommand [code=" + code + ", status=" + status + ", message=" + message + ", circulationType()="
				+ circulationType() + ", getCommand()=" + getCommand() + ", getBusinessGroup()=" + getBusinessGroup()
				+ ", getPacketType()=" + getPacketType() + "]";
	}

	/**
	 * 内置状态码
	 * @author 38079
	 *
	 */
	public static class ResoultCode{
		/**
		 * 上传相关状态码
		 * @author 38079
		 *
		 */
		public static class Upload{
			/**
			 * 客户端上传状态 c-s
			 */
			public static final int CLIENT_UPLOAD=4;
			
		}
		/**
		 * 下载相关的状态码
		 * @author 38079
		 *
		 */
		public static class Download{
			/**
			 * 客户端下载阶段 c-s
			 */
			public static final int CLIENT_DOWNLOAD=6;
			
		}
		
		/**
		 * 通用状态码
		 * @author 38079
		 *
		 */
		public static class Common{
			/**
			 * 通用双向反馈,双向，比较少用
			 */
			public static final int COMM_RESOULT=0;
			/**
			 * 服务端接收状态 s-c
			 */
			public static final int SERVER_RECEIVE=-4;
			/**
			 * 服务端文件校验阶段 s-c
			 */
			public static final int SERVER_CHECK=-5;
			
			/**
			 * 客户端文件校验阶段
			 */
			public static final int CLIENT_CHECK=5;
			
			/**
			 * 服务端推送阶段 s-c
			 */
			public static final int SERVER_PUSHFILE=-6;
		}
	}
	
}
